package team.scau.laboratory_repair.service;

import com.querydsl.core.QueryResults;
import com.querydsl.core.types.dsl.EntityPathBase;
import com.querydsl.jpa.impl.JPAQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.scau.laboratory_repair.common.base.BaseService;
import team.scau.laboratory_repair.common.page.MyPage;
import team.scau.laboratory_repair.modules.analysis.piechart.PieDataBean;
import team.scau.laboratory_repair.modules.po.QRecord;
import team.scau.laboratory_repair.modules.po.QRepairClassify;
import team.scau.laboratory_repair.modules.po.Record;
import team.scau.laboratory_repair.modules.repository.RecordRepository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: ${todo}
 * @Author: wxz1997
 * @Dated 18-7-27下午1:33
 */
@Service
public class RecordService extends BaseService<Record, RecordRepository> {

    @Autowired
    RecordRepository recordRepository;

    @Autowired
    RepairClassifyService repairClassifyService;

    private QRecord qRecord = QRecord.record;

    public List<Object[]> listFaultClassification(){
        return recordRepository.listTypeCountBar();
    }

    @Override
    public RecordRepository getRepository() {
        return recordRepository;
    }

    @Override
    public EntityPathBase<Record> getQBase() {
        return qRecord;
    }

    public List<PieDataBean> listOrByExample(){
        List<Object[]> objectsList = recordRepository.listTypeCountPie();
        List<PieDataBean> pieDataBeanList=new ArrayList<>();


        for(Object[] objects:objectsList){
            BigInteger value=(BigInteger)objects[0];
            String name=(String )objects[1];
            pieDataBeanList.add(new PieDataBean(value,name));
        }

//        QueryResults<Long> longQueryResults = queryFactory.select(qRecord.id.count())
//                .select(qRecord.classify)
//                .from(qRecord)
//                .where(qRecord.valid.eq(true))
//                .groupBy(qRecord.classify)
//                .orderBy(qRecord.classify.asc())
//                .fetchResults();
//        List<String> results1 = queryFactory.select(qRepairClassify.name)
//                .from(qRecord)
//                .leftJoin(qRepairClassify)
//                .on(qRecord.classify.eq(qRepairClassify.id))
//                .where(qRecord.valid.eq(true))
//                .groupBy(qRecord.classify, qRepairClassify.name)
//                .orderBy(qRecord.classify.asc())
//                .fetchResults().getResults();
//        List<Long> results = longQueryResults.getResults();
//        HashMap<Long,String> resultHashMap=new HashMap<>();
//        for(int i=0;i<results.size();i++){
//
//            pieDataBeanList.add(new PieDataBean(results.get(i),results1.get(i)));
//        }
        return pieDataBeanList;
    }

    public QueryResults<Record> listOrByExample(Record record, MyPage page) {
        JPAQuery<Record> jpaQuery = queryFactory.select(qRecord).from(qRecord);
        if (record != null) {
            if (record.getArea() != null) {
                jpaQuery = jpaQuery.where(qRecord.area.eq(record.getArea()));
            }
            if (StringUtils.isNotBlank(record.getEquipmentId())) {
                jpaQuery = jpaQuery.where(qRecord.equipmentId.eq(record.getEquipmentId()));
            }
            if (StringUtils.isNotBlank(record.getWechatOpenid())){
                jpaQuery = jpaQuery.where(qRecord.wechatOpenid.eq(record.getWechatOpenid()));
            }
        }
        jpaQuery = jpaQuery.where(qRecord.valid.eq(true));
        if (page != null) {
            jpaQuery = jpaQuery.orderBy(page.getSort()).offset(page.getPage()).limit(page.getSize());
        }
        return jpaQuery.fetchResults();
    }

    public List<Record> listByAreaIds(List<Long> ids) {
        if(ids == null || ids.size() == 0){
            return new ArrayList<>();
        }
        JPAQuery<Record> jpaQuery = queryFactory.select(qRecord).from(qRecord)
                .where(qRecord.area.in(ids))
                .where(qRecord.valid.eq(true));
        return jpaQuery.fetch();
    }
    public List<Object[]> listRegistrantCount(){

        return recordRepository.listRegistrantCount();
    }
    public List<Object[]> listAreaCount(){

        return recordRepository.listAreaCount();
    }

}
